<?php
/**
 * CK框架 session类
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class session
{
	/**
	 * 是否开启了session
	 * @var bool
	 */
	private static $isInit = false;
	/**
	 * 设置SESSION保存目录
	 * @var string
	 */
	private static $sessionDir = '';
	/**
	 * 开启session
	 */
	public static function start()
	{
		if(self::$isInit)
		{
			return;
		}
		session_start();
		$expireTime = config::$configObject->SESSION->SESSION_EXPIRE;
		//设置过期时间
		if($expireTime)
		{
			ini_set('session.gc_maxlifetime', $expireTime);
		}
		self::$sessionDir = CK_APPLICATION_DATA_DIR.config::$configObject->SESSION->SESSION_DIR;
		if(self::$sessionDir)
		{
			session_save_path(self::$sessionDir);
		}
		self::$isInit = true;
	}
	/**
	 * 设置session
	 * @param string $name 键
	 * @param mixed $value 值
	 */
	public static function set($name,$value)
	{
		$_SESSION[$name] = $value;
	}
	/**
	 * 获取
	 * @param string $name 键
	 */
	public static function get($name)
	{
		return isset($_SESSION[$name]) ? $_SESSION[$name] : false;
	}
	/**
	 * 删除
	 * @param string $name 键
	 */
	public static function del($name)
	{
		if(isset($_SESSION[$name]))
		{
			unset($_SESSION[$name]);
		}
	}
	/**
	 * 清除服务器上所有的大于$maxTime时间的session
	 * @param int $maxTime 生存时间
	 */
	public function gc($maxTime)
	{
		if(!self::$sessionDir)
		{
			return;
		}
		foreach (glob(self::$sessionDir.'/*') as $fileName) 
		{
		    if (filemtime($fileName) + $maxTime < SYSTIME) 
		    {
		      @unlink($fileName);
		    }
		}
		return true;
	}
	/**
	 * 销毁session
	 */
	public static function destroy()
    {
        unset($_SESSION);
        session_destroy();
    }
}